package util;

import com.alibaba.fastjson2.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @Description:
 * @Author: Mr.Liu
 * @Date: created in 16:56 2023/3/6
 * @Modified By:
 */
@Slf4j
public  class SecurityUserUtil {
    /**
     * 获取用户信息
     * @return
     */
    public static XcUser getUser(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof String){
            String principalce=principal.toString();
            XcUser xcUser=null;
            //将json转化成对象
            try {
                 xcUser = JSON.parseObject(principalce, XcUser.class);
            }catch (Exception e){
                log.debug("解析jwt用户身份,无法将他转换成XcUser对象:{}",principalce);
            }
            return xcUser;
        }
        return null;
    }


@Data
    public static class XcUser implements Serializable {

        private static final long serialVersionUID = 1L;

        private String id;

        private String username;

        private String password;

        private String salt;

        private String name;
        private String nickname;
        private String wxUnionid;
        private String companyId;
        /**
         * 头像
         */
        private String userpic;

        private String utype;

        private LocalDateTime birthday;

        private String sex;

        private String email;

        private String cellphone;

        private String qq;

        /**
         * 用户状态
         */
        private String status;

        private LocalDateTime createTime;

        private LocalDateTime updateTime;


    }
}
